﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuestPrototype.Screen_Manager
{
    public enum ScreenState
    {
        Active,
        ShutDown,
        Hidden
    }
    public class ScreenManager
    {
        private List<BaseScreen> Screens;
        private List<BaseScreen> NewScreens;
        private Screens.Debug DebugScreen = new Screens.Debug();

        public ScreenManager()
        {
            Screens = new List<BaseScreen>();
            NewScreens = new List<BaseScreen>();
            AddScreen(DebugScreen);
        }

        public void Update()
        {
            DebugScreen.Screens = "Screens: ";

            //GENERATE LIST OF DEAD SCREENS FOR REMOVAL
            List<BaseScreen> RemoveScreens = new List<BaseScreen>();
            foreach (BaseScreen FoundScreen in Screens)
            {
                if (FoundScreen.State == ScreenState.ShutDown)
                {
                    RemoveScreens.Add(FoundScreen);
                }
                else
                {
                    DebugScreen.Screens += FoundScreen.Name + ", ";
                    FoundScreen.Focused = false;
                }

            }

            // REMOVE DEAD SCREENS
            foreach (BaseScreen FoundScreen in RemoveScreens)
            {
                Screens.Remove(FoundScreen);

            }

            // ADD NEW SCREENS TO MANAGER LIST
            foreach (BaseScreen FoundScreen in NewScreens)
            {
                Screens.Add(FoundScreen);

            }

            NewScreens.Clear();

            // RESET DEBUG SCREEN TO TOP OF LIST
            Screens.Remove(DebugScreen);
            Screens.Add(DebugScreen);

            // CHECK SCREEN FOCUS
            DebugScreen.FocusScreen = "Focused Screen: ";
            if (Screens.Count > 0)
            {
                for (int i = Screens.Count - 1; i >= 0; i--)
                {
                    if (Screens.ElementAt(i).GrabFocus == true)
                    {
                        Screens.ElementAt(i).Focused = true;
                        DebugScreen.FocusScreen += Screens.ElementAt(i).Name;
                        break;
                    }
                }
            }

            // HANDLE INPUT FOR FOCUSED SCREEN
            foreach (BaseScreen FoundScreen in Screens)
            {
                if (Globals.WindowFocused)
                {
                    FoundScreen.HandleInput();
                }
                FoundScreen.Update();
            }

        }

        public void Draw()
        {
            foreach (BaseScreen FoundScreen in Screens)
            {
                if (FoundScreen.State == ScreenState.Active)
                {
                    FoundScreen.Draw();
                }
            }
        }


        public void AddScreen(BaseScreen screen)
        {
            NewScreens.Add(screen);
        }

    }
}
